package com.qyhl.webtv.module_broke.scoop.detail;

import com.google.gson.Gson;
import com.qyhl.webtv.basiclib.utils.network.EasyHttp;
import com.qyhl.webtv.basiclib.utils.network.callback.SimpleCallBack;
import com.qyhl.webtv.basiclib.utils.network.exception.ApiException;
import com.qyhl.webtv.basiclib.utils.network.model.ApiResult;
import com.qyhl.webtv.commonlib.common.CommonUtils;
import com.qyhl.webtv.commonlib.entity.circle.ScoopListBean;
import com.qyhl.webtv.module_broke.common.BrokeUrl;

/**
 * Created by qyhl on 2017/12/13.
 */

public class ScoopDetailModel implements ScoopDetailContract.ScoopDetailModel {
    private ScoopDetailPresenter mPresenter;

    public ScoopDetailModel(ScoopDetailPresenter mPresenter) {
        this.mPresenter = mPresenter;
    }


    /**
     * 获取详情
     *
     * @param brokeId
     */
    @Override
    public void getDetail(String brokeId) {
        String userName = CommonUtils.getInstance().getUserName();

        EasyHttp.get(BrokeUrl.GET_SCOOP_DETAIL)
                .params("siteId", CommonUtils.getInstance().getSite() + "")
                .params("brokeId", brokeId)
                .params("userName", userName)
                .execute(new SimpleCallBack<ScoopListBean>() {
                    @Override
                    public void onError(ApiException e) {
                        mPresenter.setError(1, "解析出错，加载失败！");
                    }

                    @Override
                    public void onSuccess(ScoopListBean scoopListBean) {
                        mPresenter.setDetail(scoopListBean);
                    }
                });
    }


    /**
     * 关注
     *
     * @param brokeId
     */
    @Override
    public void addAttention(String brokeId) {
        String userName = CommonUtils.getInstance().getUserName();
        EasyHttp.get(BrokeUrl.ADD_ATTENTION)
                .params("userName", userName)
                .params("borkeId", brokeId)
                .execute(new SimpleCallBack<String>() {
                    @Override
                    public void onError(ApiException e) {
                        mPresenter.addError("网络异常，关注失败！");
                    }

                    @Override
                    public void onSuccess(String s) {
                        ApiResult apiResult = new Gson().fromJson(s,ApiResult.class);
                        if (apiResult.getCode() == 200) {
                            mPresenter.addSuccess();
                        } else {
                            mPresenter.addError("解析异常，关注失败！");
                        }
                    }
                });


    }

    /**
     * 取消关注
     *
     * @param brokeId
     */
    @Override
    public void cancelAttention(String brokeId) {
        String userName = CommonUtils.getInstance().getUserName();

        EasyHttp.get(BrokeUrl.CANCEL_ATTENTION)
                .params("userName", userName)
                .params("borkeId", brokeId)
                .execute(new SimpleCallBack<String>() {
                    @Override
                    public void onError(ApiException e) {
                        mPresenter.cancelError("网络异常，取消失败！");
                    }

                    @Override
                    public void onSuccess(String s) {
                        ApiResult apiResult = new Gson().fromJson(s,ApiResult.class);
                        if (apiResult.getCode() == 200) {
                            mPresenter.cancelSuccess();
                        } else {
                            mPresenter.cancelError("解析异常，取消失败！");
                        }
                    }
                });

    }
}
